from django.views.generic.base import View
from django.shortcuts import render
from django.db.models import Q
from django.http import JsonResponse
from django.conf import settings
from pure_pagination import Paginator, PageNotAnInteger
from utils.mixin_utils import LoginRequiredMixin
from utils.form_utils import get_form_errors
from systems.models import Role
from systems.forms import RoleForm
from systems.views.dept import get_dept_tree2


class ListView(LoginRequiredMixin, View):
    def get(self, request):
        v_search = request.GET.get('search')
        if v_search:
            v_search = v_search.strip()
            v_objs = Role.objects.filter(Q(name__icontains=v_search) | Q(key__icontains=v_search),
                                         is_delete=0)
        else:
            v_objs = Role.objects.filter(is_delete=0)

        # 分页功能实现
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(v_objs, per_page=settings.PAGE_NUM, request=request)
        p_objs = p.page(page)
        p_start = (int(page) - 1) * settings.PAGE_NUM  # 避免分页后每行数据序号从1开始
        return render(request, 'systems/role_list.html',
                      {'p_objs': p_objs, 'p_start': p_start, 'p_sum': v_objs.count,
                       'h_search': v_search})


class AddView(LoginRequiredMixin, View):
    def get(self, request):
        return render(request, 'systems/role.html', {'h_dept_belongs': get_dept_tree2()})

    def post(self, request):
        v_form = RoleForm(request.POST)
        if v_form.is_valid():
            new_obj = v_form.save(commit=False)
            new_obj.creator = request.user.username
            new_obj.modifier = request.user.username
            # 数据归属部门不填，则默认为当前创建用户的部门ID
            if new_obj.dept_belong_id is None:
                new_obj.dept_belong_id = request.user.dept_id
            new_obj.save()
            return JsonResponse({'code': '200', 'msg': '新增成功'})
        else:
            return JsonResponse({'code': '444', 'msg': get_form_errors(v_form)})


class DetailView(LoginRequiredMixin, View):
    def get(self, request):
        v_obj = Role.objects.get(id=request.GET.get('id'))
        return render(request, 'systems/role.html', {'h_obj': v_obj,
                                                     'h_dept_belongs': get_dept_tree2(v_obj.dept_belong_id)})


class ModifyView(LoginRequiredMixin, View):
    def post(self, request):
        v_obj = Role.objects.get(id=request.POST.get('id'))
        v_form = RoleForm(request.POST, instance=v_obj)
        if v_form.is_valid():
            old_obj = v_form.save(commit=False)
            old_obj.modifier = request.user.username
            # 数据归属部门不填，则默认为当前创建用户的部门ID
            if old_obj.dept_belong_id is None:
                old_obj.dept_belong_id = request.user.dept_id
            old_obj.save()
            return JsonResponse({'code': '200', 'msg': '修改成功'})
        else:
            return JsonResponse({'code': '444', 'msg': get_form_errors(v_form)})


class DeleteView(LoginRequiredMixin, View):
    def get(self, request, i_id):
        v_user = Role.objects.get(id=i_id)
        v_user.delete()
        return JsonResponse({'code': '200', 'msg': '删除成功'})
